package cn.com.utt.unms.mapper;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import cn.com.utt.unms.pojo.Log;
import cn.com.utt.unms.vo.LogSee;

public interface LogMapper {
	@Insert("insert into log(user_id,ipaddress,content,state,date)values(#{userId},#{ipaddress},#{content},#{state},#{date})")
	@Results({  
		@Result(id=true, column="id", property="id"),  
		@Result(column="user_id", property="userId"),  
		@Result(column="ipaddress", property="ipaddress"),  
		@Result(column="content", property="content"),  
		@Result(column="state", property="state"),  
		@Result(column="date", property="date"), 
	})
	void save(Log log);
	@Select({"<script>",	
		"SELECT log.id,user.username,log.ipaddress,log.content,log.state,log.date ", 
	    "FROM log LEFT JOIN user on log.user_id=user.id",
	    "WHERE FIND_IN_SET(user_id,#{uid})",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	
	    "</script>"})
	public List<LogSee> queryLogByuserId(Map<String,Object> map);
	@Select({"<script>",
		"SELECT log.id,user.username,log.ipaddress,log.content,log.state,log.date ", 
	    "FROM log LEFT JOIN user on log.user_id=user.id",
	    "WHERE FIND_IN_SET(user_id,#{uid})",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	    "</script>"})
	public List<LogSee> queryLogByuserIdAll(Map<String,Object> map);
	@Select("select id from user where parent_id=#{userId}")
	public List<Long> queryLogChildAll(long userId);
	@Delete("delete from log where user_id=#{userId}")
	public void delete(long userId);
}